package com.dwyinyuan.interfaces.facade;

import com.dwyinyuan.domain.aggregate_file.service.*;
import com.dwyinyuan.infrastructure.util.enums.*;
import com.dwyinyuan.infrastructure.util.exception.*;
import com.dwyinyuan.interfaces.dto.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.*;

import javax.annotation.*;
import javax.validation.constraints.*;


/**
 * 微信公众号：笛舞音缘
 * <p>
 * 文件服务Controller
 * <p>
 * Created by andy  on 2020-12-16.
 */
@RestController
@RequestMapping("/api/files")
@Slf4j
@Api(value = "文件服务Controller", tags = {"文件服务Controller"}, protocols = "http, https", hidden = false)
public class FileController {

    @Resource(name = "localFileServiceImpl")
    private FileService fileService;

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public ResponseResult<String> upload(@NotNull MultipartFile file) {

        // 文件上传
        try {

            fileService.upload(file.getInputStream(), file.getOriginalFilename());
        } catch (Exception e) {

            log.error("文件上传失败！", e);
            throw new BusinessException(ErrorCodeEnum.FILE_UPLOAD_FAILURE, e);
        }

        return ResponseResult.success(file.getOriginalFilename());
    }

}
